/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * loginPanel.java
 *
 * Created on 11-12-2010, 17:53:01
 */
package UILayer.Screens;

import ControlLayer.GlobalInstances;
import ControlLayer.ILoginStateChangedEventListener;
import ControlLayer.LoginStateChangedEvent;
import UILayer.MainGuiView;
import java.awt.Color;
import org.jdesktop.application.Action;

/**
 *
 * @author Moulde
 */
public class loginPanel extends javax.swing.JPanel
{
    MainGuiView mainView;
    final String usernameValue = "Brugernavn";
    final String passwordValue = "Kodeord";
    final String loginHelpTextValue = "Skriv dit brugernavn og kodeord";
    final String loginSuccessMessage = "Klik her hvis du ikke er %s";

    /** Creates new form loginPanel */
    public loginPanel(MainGuiView mainView)
    {
        initComponents();
        this.mainView = mainView;

        GlobalInstances.getInstance().addILoginStateChangedEventListener(new ILoginStateChangedEventListener() {

            public void LoginStateChangedEventOccurred(LoginStateChangedEvent evt)
            {
                SetControlVisibility(evt.isLoggedIn());
                if (evt.isLoggedIn())
                {
                    lblAlreadyLoggedIn2.setText(String.format(loginSuccessMessage, evt.getEmployee().getName()));
                }
            }
        });

        boolean isLoggedIn = GlobalInstances.getInstance().isLoggedIn();
        SetControlVisibility(isLoggedIn);

        txtPassword.setText(passwordValue);
        txtUsername.setText(usernameValue);

        lblLoginTryStatus.setText(loginHelpTextValue);
    }

    private void SetControlVisibility(boolean value)
    {
        pnlAlreadyLoggedIn.setVisible(value);
        lblAlreadyLoggedIn1.setVisible(value);
        lblAlreadyLoggedIn2.setVisible(value);

        pnlLoginControls.setVisible(!value);
    }

    @Action
    public void Login()
    {
        String username = txtUsername.getText();
        String password = txtPassword.getText();

        if (!GlobalInstances.getInstance().logIn(username, password))
        {
            lblLoginTryStatus.setText("Dit brugernavn eller dit password er forkert, prøv igen");
            lblLoginTryStatus.setForeground(Color.red);
        }
        else
        {
            txtPassword.setText("");
            txtUsername.setText("");
            lblLoginTryStatus.setText(loginHelpTextValue);
            lblLoginTryStatus.setForeground(Color.black);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlAlreadyLoggedIn = new javax.swing.JPanel();
        lblAlreadyLoggedIn1 = new javax.swing.JLabel();
        lblAlreadyLoggedIn2 = new javax.swing.JLabel();
        pnlLoginControls = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        btnTryLogin = new javax.swing.JButton();
        lblLoginTryStatus = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlAlreadyLoggedIn.setName("pnlAlreadyLoggedIn"); // NOI18N

        lblAlreadyLoggedIn1.setFont(new java.awt.Font("Tahoma", 0, 18));
        lblAlreadyLoggedIn1.setText("Du er nu logget ind");
        lblAlreadyLoggedIn1.setName("lblAlreadyLoggedIn1"); // NOI18N

        lblAlreadyLoggedIn2.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblAlreadyLoggedIn2.setForeground(new java.awt.Color(102, 102, 102));
        lblAlreadyLoggedIn2.setText("Klik her hvis du ikke er Malte Hansen");
        lblAlreadyLoggedIn2.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblAlreadyLoggedIn2.setName("lblAlreadyLoggedIn2"); // NOI18N
        lblAlreadyLoggedIn2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAlreadyLoggedIn2MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout pnlAlreadyLoggedInLayout = new javax.swing.GroupLayout(pnlAlreadyLoggedIn);
        pnlAlreadyLoggedIn.setLayout(pnlAlreadyLoggedInLayout);
        pnlAlreadyLoggedInLayout.setHorizontalGroup(
            pnlAlreadyLoggedInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAlreadyLoggedInLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlAlreadyLoggedInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAlreadyLoggedIn1)
                    .addComponent(lblAlreadyLoggedIn2))
                .addContainerGap(223, Short.MAX_VALUE))
        );
        pnlAlreadyLoggedInLayout.setVerticalGroup(
            pnlAlreadyLoggedInLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAlreadyLoggedInLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblAlreadyLoggedIn1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAlreadyLoggedIn2)
                .addContainerGap(207, Short.MAX_VALUE))
        );

        add(pnlAlreadyLoggedIn, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 440, 260));

        pnlLoginControls.setName("pnlLoginControls"); // NOI18N
        pnlLoginControls.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblTitle.setFont(new java.awt.Font("Tahoma", 0, 18));
        lblTitle.setText("Log ind");
        lblTitle.setName("lblTitle"); // NOI18N
        pnlLoginControls.add(lblTitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        txtUsername.setText("Brugernavn");
        txtUsername.setName("txtUsername"); // NOI18N
        txtUsername.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtUsernameMouseClicked(evt);
            }
        });
        txtUsername.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtUsernameFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtUsernameFocusLost(evt);
            }
        });
        pnlLoginControls.add(txtUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 34, 160, -1));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(loginPanel.class, this);
        btnTryLogin.setAction(actionMap.get("Login")); // NOI18N
        btnTryLogin.setName("btnTryLogin"); // NOI18N
        pnlLoginControls.add(btnTryLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 110, 88, 28));

        lblLoginTryStatus.setText("jLabel1");
        lblLoginTryStatus.setName("lblLoginTryStatus"); // NOI18N
        pnlLoginControls.add(lblLoginTryStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 90, -1, -1));

        txtPassword.setText("jPasswordField1");
        txtPassword.setName("txtPassword"); // NOI18N
        txtPassword.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPasswordFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPasswordFocusLost(evt);
            }
        });
        pnlLoginControls.add(txtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 60, 160, -1));

        add(pnlLoginControls, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, 426, 140));
    }// </editor-fold>//GEN-END:initComponents

    private void txtUsernameMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_txtUsernameMouseClicked
    {//GEN-HEADEREND:event_txtUsernameMouseClicked
    }//GEN-LAST:event_txtUsernameMouseClicked

    private void txtUsernameFocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_txtUsernameFocusGained
    {//GEN-HEADEREND:event_txtUsernameFocusGained
        if (txtUsername.getText().equals(usernameValue))
        {
            txtUsername.setText("");
        }
    }//GEN-LAST:event_txtUsernameFocusGained

    private void txtUsernameFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_txtUsernameFocusLost
    {//GEN-HEADEREND:event_txtUsernameFocusLost
        if (txtUsername.getText().equals(""))
        {
            txtUsername.setText(usernameValue);
        }
    }//GEN-LAST:event_txtUsernameFocusLost

    private void lblAlreadyLoggedIn2MouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lblAlreadyLoggedIn2MouseClicked
    {//GEN-HEADEREND:event_lblAlreadyLoggedIn2MouseClicked
        GlobalInstances.getInstance().logOut();
    }//GEN-LAST:event_lblAlreadyLoggedIn2MouseClicked

    private void txtPasswordFocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_txtPasswordFocusGained
    {//GEN-HEADEREND:event_txtPasswordFocusGained
        if (txtPassword.getText().equals(passwordValue))
        {
            txtPassword.setText("");
        }
    }//GEN-LAST:event_txtPasswordFocusGained

    private void txtPasswordFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_txtPasswordFocusLost
    {//GEN-HEADEREND:event_txtPasswordFocusLost
        if (txtPassword.getText().equals(""))
        {
            txtPassword.setText(passwordValue);
        }
    }//GEN-LAST:event_txtPasswordFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTryLogin;
    private javax.swing.JLabel lblAlreadyLoggedIn1;
    private javax.swing.JLabel lblAlreadyLoggedIn2;
    private javax.swing.JLabel lblLoginTryStatus;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JPanel pnlAlreadyLoggedIn;
    private javax.swing.JPanel pnlLoginControls;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
